package cn.weiyang.biz;

import cn.weiyang.dao.AccountDao;
import cn.weiyang.dao.RecordDao;
import cn.weiyang.model.Account;
import cn.weiyang.model.Record;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017-7-15.
 */
public class AccountBizImpl implements AccountBiz {
    private AccountDao accountDao;
    private RecordDao recordDao;
    /**
     * 根据主键查询账户信息
     * @param account_id
     * @return
     */
    public Account get(long account_id) {
        return accountDao.get(account_id);
    }
    /**
     * 添加账户信息
     * @param account
     */
    public void addAccount(Account account) {
        accountDao.addAccount(account);
    }
    /**
     * 根据主键删除账户信息
     * @param account_id
     */
    public void delAccount(long account_id) {
        accountDao.delAccount(account_id);
    }
    /**
     * 修改账户信息
     * @param account
     */
    public void updateAccount(Account account) {
        accountDao.updateAccount(account);
    }
    /**
     * 查询所有账户信息
     * @return
     */
    public List<Account> findAllAccount(String hql) {
        return accountDao.findAllAccount(hql);
    }
    /**
     * 转账(转账时如果出现问题，会发生回滚)
     * @param fromaccount 转出账户
     * @param toaccount  转入账户
     * @param money  转账金额
     * @return
     */
    public void doTransferAccount(Account fromaccount, Account toaccount, float money) {
        try {
            /*修改转账人账户*/
            fromaccount.setMoney(fromaccount.getMoney()-money);
            accountDao.updateAccount(fromaccount);
            /*修改收款人账户*/
            toaccount.setMoney(toaccount.getMoney()+money);
            accountDao.updateAccount(toaccount);
            /*增加交易记录*/
            Record record=new Record(fromaccount,toaccount,new Date(),"转账"+money);
            recordDao.addRecord(record);
        }catch (Exception e){
            throw new RuntimeException();
        }

    }

    public AccountDao getAccountDao() {
        return accountDao;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public RecordDao getRecordDao() {
        return recordDao;
    }

    public void setRecordDao(RecordDao recordDao) {
        this.recordDao = recordDao;
    }
}
